from public.public import *


def send_resource(left, top, x, y, food_num, wood_num, mine_num, iron_num, gold_num,
                  translate_type=Config.JiaoChang.ZiZhongText, num=10000, *args, **kwargs):
    """
    向目标城池运输指定资源
    :param left: App的left坐标
    :param top: App的top坐标
    :param x: 城池的x坐标
    :param y: 城池的y坐标
    :param food_num: 粮食数量
    :param wood_num: 木材数量
    :param mine_num: 石料数量
    :param iron_num: 铁锭数量
    :param gold_num: 黄金数量
    :param translate_type: 指定兵种运输，默认是辎重运输
    :param num: 运输兵种数量
    :return:
    """
    # 输入坐标打开城市命令界面
    chose_city(left, top, x, y, step=2)
    # 点击运输按钮
    _ = Config.DiTuConfig.TranslateButton
    mouse_move_to_and_click(left + _[0], top + _[1])

    # 选择并输入辎重车数量
    time.sleep(1)
    _ = translate_type
    mouse_move_to_and_click(left + _[0], top + _[1])

    clear_and_send_keys(num)

    # 选择并输入粮食数量
    if food_num:
        time.sleep(1)
        _ = Config.JiaoChang.FoodText
        mouse_move_to_and_click(left + _[0], top + _[1])

        clear_and_send_keys(food_num)

    # 选择并输入木材数量
    if wood_num:
        time.sleep(1)
        _ = Config.JiaoChang.WoodText
        mouse_move_to_and_click(left + _[0], top + _[1])

        clear_and_send_keys(wood_num)

    # 选择并输入石料数量
    if mine_num:
        time.sleep(1)
        _ = Config.JiaoChang.MineText
        mouse_move_to_and_click(left + _[0], top + _[1])

        clear_and_send_keys(mine_num)

    # 选择并输入铁锭数量
    if iron_num:
        time.sleep(1)
        _ = Config.JiaoChang.IronText
        mouse_move_to_and_click(left + _[0], top + _[1])

        clear_and_send_keys(iron_num)

    # 选择并输入黄金数量
    if gold_num:
        time.sleep(1)
        _ = Config.JiaoChang.GoldText
        mouse_move_to_and_click(left + _[0], top + _[1])
        clear_and_send_keys(gold_num)

    # 点击出征进行派遣
    _ = Config.JiaoChang.ChuZheng
    mouse_move_to_and_click(left + _[0], top + _[1])

    # 关闭校场界面
    time.sleep(1)
    _ = Config.JiaoChang.CloseButton1
    mouse_move_to_and_click(left + _[0], top + _[1])


def resource_translate(food_num, wood_num, mine_num, iron_num, gold_num, translate_type=Config.JiaoChang.ZiZhongText,
                       num=10000, sleep_time=5, *args, **kwargs):
    """
    临时运输资源

    :param food_num: 粮食数量
    :param wood_num: 木材数量
    :param mine_num: 石料数量
    :param iron_num: 铁锭数量
    :param gold_num: 黄金数量
    :param translate_type: 运输兵种
    :param num: 运输兵种数量
    :param sleep_time: 没做城池间隔时间
    """
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)

    cities = get_soldier_cities(control)
    translate_city = get_translate_city(control)
    if get_solider_cities_reserve_config(control):
        cities.reverse()
    for _ in cities:
        if _ == translate_city:
            continue
        # 打开地图
        open_map(left, top)
        send_resource(left, top, _[0], _[1],
                      food_num, wood_num, mine_num, iron_num, gold_num,
                      translate_type=translate_type,
                      num=num
                      )
        logger.info("已经向城池（{},{}）派出运输车".format(_[0], _[1]))
        time.sleep(sleep_time)


def translate_by_solider_type(solider_type, solder_num, double=False, translate_type=Config.JiaoChang.ZiZhongText,
                              num=10000, sleep_time=5, *args, **kwargs):
    times = kwargs.get('times', 1)
    food = solider_type.get("Food") * solder_num * times
    wood = solider_type.get("Wood") * solder_num * times
    mine = solider_type.get("Mine") * solder_num * times
    iron = solider_type.get("Iron") * solder_num * times
    if double:
        food *= 2
        wood *= 2
        mine *= 2
        iron *= 2
    resource_translate(food, wood, mine, iron, 0, translate_type=translate_type, num=num,
                       sleep_time=sleep_time, *args, **kwargs)


def friendly_send_resource(food_num, wood_num, mine_num, iron_num, gold_num,
                           translate_type=Config.JiaoChang.ZiZhongText, num=10000, *args, **kwargs):
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)

    cities = get_friendly_cities(control)
    for _ in cities:
        # 打开地图
        open_map(left, top)
        send_resource(left, top, _[0], _[1],
                      food_num, wood_num, mine_num, iron_num, gold_num,
                      translate_type=translate_type,
                      num=num,
                      *args, **kwargs)

def qingqi_func(*args, **kwargs):
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)

    # 打开地图
    open_map(left, top)

    cities = get_soldier_cities(control)
    translate_city = get_translate_city(control)
    for _ in cities:
        if _ == translate_city:
            continue
        send_resource(left, top, _[0], _[1], 500 * 10000, 0, 0, 0,
                      Config.JiaoChang.QingQiText, 5 * 10000, *args, **kwargs)


if __name__ == "__main__":
    # translate_by_solider_type(Config.SolderResource.QingQiType, 12000, num=10000, double=True)
    # translate_by_solider_type(Config.SolderResource.QingQiType, 12000, num=19000, double=True, times=2)
    translate_by_solider_type(Config.SolderResource.QingQiType, 12000, num=5000, double=False)
    # translate_by_solider_type(Config.SolderResource.ZiZhongType, 12000, num=5000, double=False)
    # translate_by_solider_type(Config.SolderResource.ChuangNuType, 6000, num=10000, double=True)
    # translate_by_solider_type(Config.SolderResource.ChuangNuType, 6000, num=20000, double=True, times=2)
    # translate_by_solider_type(Config.SolderResource.ChuangNuType, 6000, num=10000, double=True)
    # translate_by_solider_type(Config.SolderResource.TieQiType, 6000, num=10000, double=True)

    # 临时运输
    # resource_translate(0 * 10000, 0 * 10000, 0 * 10000, 0 * 10000, 1000*10000, num=5000)
    # resource_translate(1300 * 10000, 1040 * 10000, 0 * 10000, 900 * 10000, 0*10000, num=10000)
    # main(1440 * 10000, 3600 * 10000, 0, 840 * 10000, 500*10000, num=10000)
    # main(0 * 10000, 0 * 10000, 0 * 10000, 0 * 10000, 1000*10000, num=2600)

    # 小号主城运输
    # friendly_send_resource(480 * 10000, 560 * 10000, 0, 480 * 10000, 0, translate_type=Config.JiaoChang.QingQiText,
    #                        num=100000)
    # friendly_send_resource(480 * 10000, 560 * 10000, 0, 480 * 10000, 0, translate_type=Config.JiaoChang.QingQiText,
    #                        num=100000)
